//获取应用实例
const app = getApp();

Page({
	data: {
    noData:false,
    bank_name:'请选择银行账户',
    bankNo:'',
    inputMoney:'',
    balance:'0'
  },
	onLoad: function() {
    this.setData({
     balance:app.globalData.balance
    })
  },

  onShow(){
    console.log(app.globalData.bankId)
    if(app.globalData.bankId.length>0){
      this.getBankMsg();
    }
  },
  inputMoney:function(e){
    this.setData({
      inputMoney: e.detail.value
    })
  },
  cashAll:function(){
    if (this.data.balance>0){
      this.setData({
        inputMoney: this.data.balance
      })
    }
  },
  cashSubmit:function(){
    var that=this;
    if (that.data.inputMoney.length<=0){
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return;
    }
    if (that.data.bankNo.length>0){
      wx.showLoading({
        title: '申请提现中',
      })
      console.log(that.data.bankNo);
      wx.request({
        url: app.globalData.api + '/index.php/wap/UserBalance/draw',
        data: {
          'token': app.globalData.token,
          account_type: that.data.bankList.bank_id,
          account: that.data.bankList,
          money: that.data.bankNo,
          truename: that.data.bankList.truename
        },
        headers: {
          'platform': '' + app.globalData.platform,
          'version': '' + app.globalData.version,
        },
        method: 'POST',
        dataType: 'json',
        success(res) {
          console.log(res)
          wx.hideLoading();
          if (res.data.code == 1) {
            wx.showToast({
              title: '申请成功',
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: '' + res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '请选择账户',
        icon:'none'
      })
    }
    
    // wx.showToast({
    //   title: '功能开发中',
    //   icon:'none'
    // })
  },
  //获取银行卡信息
  getBankMsg() {
    wx.showLoading({
      title: '获取数据中',
    })
    var _this = this;
    var cardNo;
    wx.request({
      url: app.globalData.api + '/index.php/wap/BankAccount/getMsg',
      data: {
        'token': app.globalData.token,
        id: app.globalData.bankId
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.code == 1) {
          _this.setData({
            bankNo: res.data.data.list.account
          });
          console.log(res.data.data)
          res.data.data.list.account = res.data.data.list.account.substring(res.data.data.list.account.length - 4, res.data.data.list.account.length); //要截取时间的字符串
            _this.setData({
              bankList: res.data.data.list,
              bank_name:res.data.data.list.bank_name,
              noData: false
            })

            console.log(_this.data.bankList)
        } else {
          _this.setData({
            bankList:'',
            bank_name: '请选择银行账户',
            noData: false
          })
        }
      }
    })
  },

});
